package com.ruoyi.consumer.controller;

import com.github.pagehelper.PageInfo;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.constant.HttpStatus;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.consumer.domain.CCompany;
import com.ruoyi.consumer.domain.CDealCompany;
import com.ruoyi.consumer.domain.CUser;
import com.ruoyi.consumer.domain.param.DealCompanyDTO;
import com.ruoyi.consumer.service.ICDealCompanyService;
import com.ruoyi.consumer.service.impl.CCompanyServiceImpl;
import com.ruoyi.consumer.service.impl.CUserServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * 公司钱包流水记录Controller
 *
 * @author ruoyi
 * @date 2023-11-17
 */
@RestController
@RequestMapping("/consumer/dealCompany")
public class CDealCompanyController extends BaseController
{
    @Autowired
    private ICDealCompanyService cDealCompanyService;
    @Autowired
    private CCompanyServiceImpl companyService;
    @Autowired
    private CUserServiceImpl userService;

    public TableDataInfo getDataTable(List<CDealCompany> list, List<?> list1)
    {
        // 使用原始 list 创建 PageInfo 对象来保留分页信息
        PageInfo<CDealCompany> pageInfo = new PageInfo<>(list);
        TableDataInfo rspData = new TableDataInfo(list1, (int)pageInfo.getTotal());
        rspData.setCode(HttpStatus.SUCCESS);
        rspData.setMsg("查询成功");
        rspData.setRows(list1);
        rspData.setTotal(new PageInfo(list).getTotal());
        return rspData;
    }

    /**
     * 查询公司钱包流水记录列表
     */
    @PreAuthorize("@ss.hasPermi('consumer:dealCompany:list')")
    @GetMapping("/list")
    public TableDataInfo list(CDealCompany cDealCompany)
    {
        List<DealCompanyDTO> list1 = new ArrayList<>();
        startPage();
        List<CDealCompany> list = cDealCompanyService.selectCDealCompanyList(cDealCompany);


        for (CDealCompany dealCompany : list) {
            //查询公司名称
            CCompany company = companyService.selectCCompanyByCompanyId(dealCompany.getCompanyId());
            //查询用户名称
            CUser user = userService.selectCUserByUserId(dealCompany.getTargetUserId());
            DealCompanyDTO dealCompanyDTO = new DealCompanyDTO(dealCompany, company, user);
            list1.add(dealCompanyDTO);
        }
        return getDataTable(list,list1);
    }


    /**
     * 导出公司钱包流水记录列表
     */
    @PreAuthorize("@ss.hasPermi('consumer:dealCompany:export')")
    @Log(title = "公司钱包流水记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CDealCompany cDealCompany)
    {
        List<CDealCompany> list = cDealCompanyService.selectCDealCompanyList(cDealCompany);
        ExcelUtil<CDealCompany> util = new ExcelUtil<CDealCompany>(CDealCompany.class);
        util.exportExcel(response, list, "公司钱包流水记录数据");
    }

    /**
     * 获取公司钱包流水记录详细信息
     */
    @PreAuthorize("@ss.hasPermi('consumer:dealCompany:query')")
    @GetMapping(value = "/{dealRecordId}")
    public AjaxResult getInfo(@PathVariable("dealRecordId") Integer dealRecordId)
    {
        return success(cDealCompanyService.selectCDealCompanyByDealRecordId(dealRecordId));
    }

    /**
     * 新增公司钱包流水记录
     */
    @PreAuthorize("@ss.hasPermi('consumer:dealCompany:add')")
    @Log(title = "公司钱包流水记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CDealCompany cDealCompany)
    {
        return toAjax(cDealCompanyService.insertCDealCompany(cDealCompany));
    }

    /**
     * 修改公司钱包流水记录
     */
    @PreAuthorize("@ss.hasPermi('consumer:dealCompany:edit')")
    @Log(title = "公司钱包流水记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CDealCompany cDealCompany)
    {
        return toAjax(cDealCompanyService.updateCDealCompany(cDealCompany));
    }

    /**
     * 删除公司钱包流水记录
     */
    @PreAuthorize("@ss.hasPermi('consumer:dealCompany:remove')")
    @Log(title = "公司钱包流水记录", businessType = BusinessType.DELETE)
	@DeleteMapping("/{dealRecordIds}")
    public AjaxResult remove(@PathVariable Integer[] dealRecordIds)
    {
        return toAjax(cDealCompanyService.deleteCDealCompanyByDealRecordIds(dealRecordIds));
    }
}
